package org.reliablesource.prism.core.tests;

import java.awt.Point;

import junit.framework.TestCase;

import org.eclipse.core.runtime.Platform;
import org.reliablesource.prism.core.IPrismAnnotation;
import org.reliablesource.prism.core.IPrismElementFactory;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;

/**
 * PrismScreenTest.java Copyright (c) 2006, Reliable Source, Inc. All Rights
 * Reserved
 * 
 * Created on: Sep 20, 2006
 * 
 * @author Dennis Park <a
 *         href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 */
public class PrismScreenTest extends TestCase {

	IPrismElementFactory factory;

	IPrismScreen screen;
	String imageFilePath;

	/**
	 * @param arg0
	 */
	public PrismScreenTest(String arg0) {
		super(arg0);
		factory = PrismCorePlugin.getDefault().getElementFactory();
		assertNotNull(factory);
		
		if(Platform.getOS().equals("linux"))
			imageFilePath = "/tmp/";
		else if(Platform.getOS().equals("win32"))
			imageFilePath = "C:\\temp\\";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		assertNull(screen);
		screen = factory.createScreen();
		assertNotNull(screen);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
		assertNotNull(screen);
		screen = null;
		assertNull(screen);
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#PrismScreen()}.
	 */
	public final void testPrismScreen() {
		assertNotNull(screen.getId());
		assertNotNull(screen.getName());
		assertNotNull(screen.getParent());
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#setImageFileType(java.lang.String)}.
	 */
	public final void testSetImageFileType() {
		assertNull("file type is already set.", screen.getImageFileType());
		screen.setImageFileType("gif");
		assertNotNull("Filetype is null", screen.getImageFileType());
		assertEquals("Filetype is not gif", screen.getImageFileType(), "gif");
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#setImageFilePath(java.lang.String)}.
	 */
	public final void testSetImageFilePath() {
		assertNotNull("Platform not supported.",imageFilePath);
		assertNull("imageFilePath is already set", screen.getImageFilePath());
		screen.setImageFilePath(imageFilePath);
		assertNotNull("Filepath is null", screen.getImageFilePath());
		assertEquals("Filepath is not " + imageFilePath, screen.getImageFilePath(), imageFilePath);
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#getAnnotation(int)}.
	 */
	public final void testGetAnnotation() {
		for(int i = 0; i < 9; i++)
			factory.createPrismAnnotation(screen);
		
		assertTrue("screen does not have 9 annotataions", screen.getAnnotations().length == 9);
	}


	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#removeAnnotation(org.reliablesource.prism.core.IPrismAnnotation)}.
	 */
	public final void testRemoveAnnotation() {
		for(int i = 0; i < 30; i++)
			factory.createPrismAnnotation(screen);
		
		int nAnnotations = screen.getAnnotationsList().size();
		Object [] annotations = screen.getAnnotations();
		assertTrue("annotations is < 1", nAnnotations > 0);
		
		for(int i = 0; i < nAnnotations; i++)
			screen.removeAnnotation((IPrismAnnotation) annotations[i]);
		
		assertTrue("annotation is > 0 after complete removal", screen.getAnnotations().length == 0);
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#addPathLocation(int, java.awt.Point)}.
	 */
	public final void testAddPathLocation() {

		assertTrue("screen already has path locations",screen.getPathLocationsList().size() == 0);

		final int newElements = 10;
		for (int i = 0; i < newElements; i++) 
			screen.addPathLocation(i, new Point(i, i));

		assertEquals("PathLocations and added locations not equal::" +screen.getPathLocationsList().size() ,screen.getPathLocationsList().size(), newElements);
		System.err.println("List size:" + screen.getPathLocationsList().size());
		
		
		for (int i = 0; i < newElements; i++) {
			Point pt = screen.getPathLocation(i);
			assertEquals(pt.x,i);
			assertEquals(pt.y,i);
		}
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#removePathLocation(int, java.awt.Point)}.
	 */
	public final void testRemovePathLocation() {
		
		assertTrue(screen.getPathLocationsList().size() == 0);

		final int newElements = 10;
		for (int i = 0; i < newElements; i++) 
			screen.addPathLocation(i, new Point(i, i));

		assertTrue(screen.getPathLocationsList().size() == newElements);
		assertFalse(screen.removePathLocation(0, new Point(500, 500)));
		assertTrue(screen.getPathLocationsList().size() == newElements);

		for (int j = 0; j < newElements; j++) 
			screen.removePathLocation(j, screen.getPathLocation(j));
		
		assertTrue(screen.getPathLocationsList().size() == 0);
	}

	/**
	 * Test method for
	 * {@link org.reliablesource.prism.core.internal.PrismScreen#getPathLocationsList()}.
	 */
	public final void testGetPathLocationsList() {
		for(int i = 0; i < 15; i++)
			screen.addPathLocation(i, new Point(i,i));
		
		assertTrue("pathLocationsList does equal the number of locations added",screen.getPathLocationsList().size() == 15);
		
		for(int i = 0; i < 15; i++){
			assertTrue("path location retrieval failed on x",screen.getPathLocation(i).x == i);
			assertTrue("path location retrieval failed on y", screen.getPathLocation(i).y == i);
		}
	}
}
